<template>
  <div>
    <el-form ref="form" :model="form" :rules="formRules" :hide-required-asterisk="true">
      <el-form-item label="部门名称" label-width="120px" prop="name">
        <el-input v-model="form.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="部门编码" label-width="120px" prop="code">
        <el-input v-model="form.code" placeholder="请输入部门编码" />
      </el-form-item>
      <el-form-item label="部门负责人" label-width="120px" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择部门负责人">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" label-width="120px" prop="introduce">
        <el-input v-model="form.introduce" placeholder="请输入部门介绍" />
      </el-form-item>
      <el-form-item style="text-align:center">
        <el-button type="primary" @click="hSubmit">确定</el-button>
        <el-button @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { getStaffList, addDepartment, getDetailsById, setDetailById } from '@/api/department'
export default {
  name: 'DeptDialog',
  props: {
    pid: {
      type: String,
      required: true
    },
    num: {
      type: Number,
      required: true
    },
    department: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义部门编码重复校验
    const validateCode = (rule, value, callback) => {
      const allCodeArr = this.department.map(item => item.code)
      if (this.num === 2) {
        const index = allCodeArr.findIndex(item => this.form.code === item)
        allCodeArr.splice(index, 1)
      }
      if (value) {
        allCodeArr.includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
      }
    }
    // 自定义部门名称重复校验
    const validateName = (rule, value, callback) => {
      const obj = this.department.find(item => item.id === this.pid)
      const existNameList = this.department.filter(item => {
        return item.pid === obj.pid && item.id !== this.pid
      }).map(item => item.name)
      existNameList.includes(value) ? callback(new Error(`名称${value}已存在`)) : callback()
    }
    return {
      form: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        manager: '', // 部门负责人名称
        name: '' // 部门名称
      },
      managerList: null,
      formRules: {
        code: [
          { required: true, message: '请填写部门编码', trigger: 'blur' },
          { max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请填写部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请填写部门介绍', trigger: 'blur' },
          { max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await getStaffList()
    this.managerList = res.data
    if (this.num === 2) {
      const Detailres = await getDetailsById(this.pid)
      this.form = Detailres.data
    }
  },
  methods: {
    hSubmit() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        if (this.num === 1) {
          await addDepartment({ ...this.form, pid: this.pid })
          this.$message.success('添加子部门成功')
        } else {
          await setDetailById(this.pid, this.form)
          console.log({ ...this.form, pid: this.pid })
          this.$message.success('修改部门信息成功')
        }
        this.$emit('addDep', false)
      })
    },
    hCancel() {
      this.$refs.form.resetFields()
      this.$emit('changeShow', false)
    }
  }
}
</script>

<style lang="scss" scoped></style>

